package org.edu.modules.growth.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.edu.common.api.vo.Result;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.growth.dto.VeJsczSummaryDTO;
import org.edu.modules.growth.entity.VeJsczSummary;
import org.edu.modules.growth.service.IVeJsczSummaryService;
import org.edu.modules.growth.vo.QueryCondition;
import org.edu.modules.growth.vo.SummaryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

@Api(tags="年终总结管理")
@RestController
@RequestMapping("jsczSummary")
@Slf4j
@AllArgsConstructor
public class VeJsczSummaryController extends BaseController<VeJsczSummary, IVeJsczSummaryService> {

    private IVeJsczSummaryService mIVeJsczSummaryService;


    /**
     * 判断当前用户信息及查询年终终结信息
     */
    @ApiOperation(value = "判断当前用户信息及查询年终终结信息")
    @PostMapping("/init")
    public Result<SummaryVo> init( ) {
        return Result.OK(mIVeJsczSummaryService.init());
    }

    /**
     * 提交或编辑
     */
    @ApiOperation(value = "提交或编辑年终总结信息")
    @PostMapping("/addOrUpdate")
    public Result<?> addOrUpdate(@RequestBody VeJsczSummaryDTO dto) throws Exception {
        if( !mIVeJsczSummaryService.isPass() ){
            return Result.error("当前时间不在年终总结提交时间范围内，无法操作!") ;
        }
        VeJsczSummary sumarry = new VeJsczSummary() ;
        BeanUtils.copyProperties(dto,sumarry);
        if( sumarry.getId() != null ){
            return Result.OK(mIVeJsczSummaryService.updateById(sumarry));
        }
        // 新增
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        sumarry.setTerminalId(1L)
                .setUserId(Long.parseLong(user.getId())).setTeacherUserId(Long.parseLong(user.getId())) ;
        mIVeJsczSummaryService.add(sumarry) ;
        return Result.OK();
    }

    /**
     * 根据id详情
     */
    @ApiOperation(value = "根据id详情")
    @PostMapping("/getDetail")
    public Result<VeJsczSummary> getDetail(@RequestParam("id") Long id) {
        return Result.OK(mIVeJsczSummaryService.getById(id));
    }

    /**
     * 已提交查询
     */
    @ApiOperation(value = "已提交分页查询")
    @PostMapping("/selectPage")
    public Result<?> selectPage(@RequestBody QueryCondition queryCondition ) {
        return Result.OK(mIVeJsczSummaryService.selectPage(queryCondition));
    }

    /**
     * 未提交查询
     */
    @ApiOperation(value = "未提交查询--年份信息必传(具体还需确认)")
    @PostMapping("/selectPageNoSubmit")
    public Result<?> selectPageNoSubmit(@RequestParam(value = "XM",required = false)String XM,@RequestParam(value = "GH",required = false)String GH,
                                      @RequestParam("year")Integer year,@RequestParam(value = "deptId",required = false)String deptId,
                                      @RequestParam(value = "zyzId",required = false)String zyzId,@RequestParam("pageNum")Integer pageNum,
                                      @RequestParam("pageSize")Integer pageSize) {
        pageNum = (pageNum-1)*pageSize ;
        return Result.OK();
    }





}
